package com.white.algebra.dao.impl;

import com.white.algebra.dao.PermissionDao;
import com.white.algebra.domain.Permission;
import com.white.algebra.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class PermissionDaoImpl implements PermissionDao {
    /**
     * 根据角色id查出没有的权限
     *
     * @param id
     * @return
     */
    @Override
    public List<Permission> findUserByIdAndAllRole(String id) throws SQLException {
        return getPermissions(id, "select * from permission where id not in (select permissionId from role_permission where roleId=?)");
    }

    /**
     * 根据角色id查询对应权限
     *
     * @param id
     * @return
     */
    @Override
    public List<Permission> findById(String id) throws SQLException {
        return getPermissions(id, "select * from permission where id in (select permissionId from role_permission where roleId=?)");
    }

    private List<Permission> getPermissions(String id, String s) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        List<Permission> permissionList=null;
        String sql = s;
        Object[] params = { id};
        permissionList= (List<Permission>) qr.query(sql,params, new BeanListHandler(Permission.class));
        return permissionList;
    }

    /**
     * 关联角色_权限表
     *
     * @param permissionId
     * @param roleId
     */
    @Override
    public void addpermission(String permissionId, String roleId) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "insert into role_permission(permissionId,roleId) values(?,?)";
        Object[] params = {permissionId,roleId};
        qr.update(sql,params);
    }

    /**
     * 根据角色与权限id删除角色_权限表
     *
     * @param permissionId
     * @param roleId
     */
    @Override
    public void deleteRole_PermissionByPermissionAndRoleId(String permissionId, String roleId) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "delete from role_permission where permissionId=? and roleId=?";
        Object[] params = {permissionId,roleId};
        qr.update(sql,params);
    }

    /**
     * 根据权限id删除对应权限
     *
     * @param id
     */
    @Override
    public void deletePermissionById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "delete from permission where id=?";
        Object[] params = {id};
        qr.update(sql,params);
    }

    /**
     * 根据权限id删除对应角色_权限
     *
     * @param id
     */
    @Override
    public void deleteRole_PermissionByPermissionId(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "delete from role_permission where permissionId=?";
        Object[] params = {id};
        qr.update(sql,params);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @Override
    public List<Permission> findAll() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        List<Permission> permissionList=null;
        String sql = "select * from permission";
        permissionList= (List<Permission>) qr.query(sql, new BeanListHandler(Permission.class));
        return permissionList;
    }

    /**
     * 添加权限
     *
     * @param permission
     */
    @Override
    public void save(Permission permission) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "insert into permission(permissionName,url) values(?,?)";
        Object[] params = {permission.getPermissionName(),permission.getUrl()};
        qr.update(sql,params);
    }

    /**
     * 根据权限id查询权限
     *
     * @return
     */
    @Override
    public Permission findPermissionById(String id) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());
        Permission permission=null;
        String sql = "select * from permission where id=?";
        Object[] params = {id};
        permission= (Permission) qr.query(sql, params,new BeanHandler(Permission.class));
        return permission;
    }
}
